
import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/framework/component/searchbar/x_state_searchbar_del_widget.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
搜索栏
Widget searchBarWidget=XStatefulWidget(()=>XComponentSearchBarState());

注意:
1.ok 在onBuild或者rebuildItemWidget中使用setXTopBarWidget()添加搜索栏,每次刷新主界面都会重新创建Widget,则需要从searchResultCallback中获得当前值记录以保持原样;
2.ok 在oncreate中使用setXTopBarWidget()添加搜索栏,则刷新主界面不会重新创建Widget,但会重新创建state,如果当前内容保持在state中,则当前内容会丢失,因此当前内容要记录到widget中,解决刷新主界面当前值不会丢失;

 */
class XComponentSearchBarState extends XState{
  String? initSearchValue;
  Color? textColor;
  String? hintText;
  Color? hintTextColor;
  Color? iconColor;
  Color? searchBarBackgroundColor;
  Color? outSideBackgroundColor;
  Widget? outsideLeftCustomWidget;
  List<Widget>? outsideRightCustomWidgets;
  List<Widget>? insideRightCustomWidgets;
  bool autofocus;//自动获得焦点,刷新页面不关闭输入法
  double height;//searchbar高度
  bool isShowLeftBackIcon;//是否显示返回按钮
  EdgeInsets? margin;

  //搜索内容实时返回
  XFunctionResultCallback<String>? searchResultCallback;
  //点击搜索
  XFunctionWidgetOnClick<String?>? onTapSearch;

  GestureTapCallback? onTap;

  bool _isShowDelWidget=false;
  TextEditingController? _textEditingController;

  final GlobalKey<XStateSearchBarDelWidget> _showSearchBarDelWidgetKey = GlobalKey();

  XComponentSearchBarState(
      {
        this.initSearchValue,
        required this.searchResultCallback, //返回当前内容,在主界面中赋值记录,防止主界面刷新时替换
        required this.onTapSearch,//点击搜索回调(搜索图标,键盘确认键)
        this.hintText,
        this.isShowLeftBackIcon=false,//是否显示返回按钮
        this.outsideLeftCustomWidget,
        this.outsideRightCustomWidgets,
        this.insideRightCustomWidgets,
        this.iconColor=XColorUtils.colorCCCCCC,
        this.textColor=XColorUtils.color333333,
        this.hintTextColor=XColorUtils.colorCCCCCC,
        this.searchBarBackgroundColor,
        this.outSideBackgroundColor,
        this.autofocus=false,//自动获得焦点,刷新页面不关闭输入法,true时初始进入页面自动弹输入法,false时初始进入页面不弹输入法(后期点击输入框重新设置自动改为true),
        this.height=48,
        this.margin,
        this.onTap,
      }
      );



  @override
  void onCreate() {
    //使用widget记录当前内容, 初始化赋值,后期刷新界面当前值则不会丢失
    widget.xStateValue1 ??= initSearchValue;

    if(!XStringUtils.isEmpty(widget.xStateValue1)){
      _isShowDelWidget=true;
    }
    //XLogUtils.printLog('---初始化_isShowDelWidget=$_isShowDelWidget,currentValue=${widget.xStateValue1}');
  }


  @override
  Widget onBuild(BuildContext context) {

    _textEditingController = TextEditingController(text:widget.xStateValue1);

    // 将光标置于末尾
    _textEditingController?.selection = TextSelection.fromPosition(TextPosition(
      affinity: TextAffinity.downstream,
      offset: _textEditingController?.text.length??0,
    ));

    List<Widget> listWidgets=[];

    if(isShowLeftBackIcon){
      listWidgets.add(IconButton(
          icon: const Icon(
            Icons.arrow_back_ios_new_rounded,
            color: Color(0xFF333333),
          ),
          onPressed: () {
            //返回上一页
            //finishPage();
          })
      );
    }

    if(outsideLeftCustomWidget!=null){
      listWidgets.add(outsideLeftCustomWidget!);
    }


    List<Widget> searchbarInnerWidgets=[];

    //搜索图标
    searchbarInnerWidgets.add(
        XWidgetUtils.getWidgetGestureDetector(
          child: XWidgetUtils.getLayoutPadding(
              padding: XWidgetUtils.getEdgeInsets(10, 5, 5, 5),
              child: XImageUtils.getImageWidgetWithAsset(
                  'x_search.png',
                  width: 15,
                  height: 15,
                  color: iconColor
              )
          ),
          onTap: (){
            if(onTapSearch!=null){
              onTapSearch!(widget.xStateValue1);
            }
          },
        )
    );

    //输入框
    searchbarInnerWidgets.add(
        XWidgetUtils.getLayoutExpanded(
            XWidgetUtils.getWidgetTextField(
                controller:_textEditingController,
                textInputAction:TextInputAction.search,
                hintText: hintText,
                autofocus: autofocus,
                style: XStyleUtils.getTextStyle(
                  fontSize: 14,
                  color: textColor,
                  lineHeight: 1.22,
                ),
                hintTextStyle: XStyleUtils.getTextStyle(
                  fontSize: 14,
                  color: hintTextColor,
                  lineHeight: 1.22,
                ),
                onChanged: (String result){
                  widget.xStateValue1=result;

                  if(XStringUtils.isEmpty(widget.xStateValue1) && _isShowDelWidget){
                    //设置隐藏删除按钮
                    _isShowDelWidget=false;
                    _showSearchBarDelWidgetKey.currentState?.showDelWidget(false);
                  }else if(!XStringUtils.isEmpty(widget.xStateValue1) && !_isShowDelWidget){
                    //设置隐藏显示按钮
                    _isShowDelWidget=true;
                    _showSearchBarDelWidgetKey.currentState?.showDelWidget(true);
                  }
                  if(searchResultCallback!=null){
                    searchResultCallback!(widget.xStateValue1);
                  }
                },
                onSubmitted: (String result){
                  widget.xStateValue1=result;
                  if(onTapSearch!=null){
                    onTapSearch!(widget.xStateValue1);
                  }
                },
                onTap:onTap,
                readOnly:onTap!=null?true:false,
            )
        )
    );

    //清除输入图标(通过_showSearchBarDelWidgetKey内部更新)
    searchbarInnerWidgets.add(
        XStatefulWidget(()=>XStateSearchBarDelWidget(
            onclick: (re) {

              //使用_textEditingController?.clear();清除已输入内容显示,替代setState();避免导致输入法无法删除bug
              _textEditingController?.clear();
              widget.xStateValue1='';
              _isShowDelWidget=false;
              if(searchResultCallback!=null){
                searchResultCallback!(widget.xStateValue1);
              }

            }),
            key: _showSearchBarDelWidgetKey,
        )
    );

    if(insideRightCustomWidgets!=null && insideRightCustomWidgets!.isNotEmpty){
      searchbarInnerWidgets.addAll(insideRightCustomWidgets!);
    }

    //
    listWidgets.add(
        XWidgetUtils.getLayoutExpanded(
            XWidgetUtils.getXLayoutRoundedRect(
              margin: XWidgetUtils.getEdgeInsets(
                  (isShowLeftBackIcon||outsideLeftCustomWidget!=null)?0:10, 0, 10, 0),
              height: 32,
              radius_topLeft: 16,
              radius_topRight: 16,
              radius_bottomRight: 16,
              radius_bottomLeft: 16,
              alignment: Alignment.center,
              backgroundColor: searchBarBackgroundColor??XColorUtils.pageBackgroundColor,
              child: XWidgetUtils.getLayoutRow(
                  children: searchbarInnerWidgets
              ),
            )
        )
    );

    //右侧自定义图标
    if(outsideRightCustomWidgets!=null && outsideRightCustomWidgets!.isNotEmpty){
      listWidgets.addAll(outsideRightCustomWidgets!);
    }
    return XWidgetUtils.getLayoutContainer(
      height: height,
      backgroundColor: outSideBackgroundColor,
      alignment: Alignment.center,
      child: XWidgetUtils.getLayoutRow(children: listWidgets),
      margin: margin,
    );
  }

  @override
  void onDestroy() {
    _textEditingController?.dispose();
  }

  @override
  void onStart() {
  }


  @override
  Future<void> updateData({int pageIndex = 1}) async{
  }



}